﻿using System;
using System.Collections.Generic;
using System.Text;

namespace SharpSoft.Geometries
{
    /// <summary>
    /// 表示一个角度制数据
    /// </summary>
    public struct Degree
    {
        private double degree;
        public Degree(double d)
        {
            degree = d;
        }

        public static implicit operator double(Degree d)
        {
            return d.degree;
        }
        public static implicit operator Degree(double d)
        {
            return new Degree(d);
        }
        /// <summary>
        /// 将角度转换为弧度
        /// </summary>
        /// <param name="d"></param>
        public static implicit operator Radian(Degree d)
        {
            return d.ToRadian();
        }
        /// <summary>
        /// 转换为弧度制
        /// </summary>
        /// <returns></returns>
        public Radian ToRadian()
        {
            return new Radian(this.degree * Math.PI / 180d);
        }

        public static bool operator ==(Degree d1, Degree d2)
        {
            return d1.Equals(d2);
        }
        public static bool operator !=(Degree d1, Degree d2)
        {
            return !d1.Equals(d2);
        }

        public static Degree operator +(Degree d1, Degree d2)
        {
            return new Degree(d1.degree + d2.degree);
        }
        public static Degree operator -(Degree d1, Degree d2)
        {
            return new Degree(d1.degree - d2.degree);
        }

        /// <summary>
        /// 将角度优化到圆周内
        /// </summary>
        /// <returns></returns>
        public Degree Optimize360()
        {
            var o = this.degree % 360;
            if (o < 0)
            {//处理负数角度
                o += 360;
            }
            return new Degree();
        }

        public override bool Equals(object obj)
        {
            if (obj is Degree d)
            {
                return d.degree == this.degree;
            }
            else if (obj is Radian r)
            {
                return this.Equals((Degree)r);
            }
            else if (obj is double dou)
            {
                return this.degree == dou;
            }
            return false;
        }

        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        public override string ToString()
        {
            return $"{degree}°";
        }
    }
}
